/*
 * Created by JFormDesigner on Mon Jul 05 21:46:10 CST 2021
 */

package com.tarena.view;
import com.tarena.pojo.Message;
import com.tarena.pojo.MessageType;
import com.tarena.pojo.User;
import com.tarena.service.ClientConnectThread;
import com.tarena.utils.ClientMessageQueue;
import com.tarena.utils.ManageUserWindowList;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author unknown
 */
public class ChatFriendsWindow extends JFrame {
    private Socket socket;
    private User currentUser;
    private ObjectOutputStream oos;
    private ClientConnectThread cst;
    private User userValue;
    private int selection = 1;
   // private User sender;

    public ChatFriendsWindow(Socket socket,User currentUser,ObjectOutputStream oos,ClientConnectThread cst) {
        this.socket=socket;
        this.currentUser =currentUser;
        this.oos = oos;
        this.cst=cst;
        initComponents();
    }

    public int getSelection() {
        return selection;
    }

    public void jlistChangeIcon() {


    }

    private void list1ValueChanged(ListSelectionEvent e) {
        if (!list1.getValueIsAdjusting()) {
           // System.out.println(list1.getSelectedValue());
            User selectedValue = (User) list1.getSelectedValue();
            if(selectedValue != null){
                userValue = selectedValue;
            }
           // System.out.println(userValue);

            ChatMainWindow window = ManageUserWindowList.findUserWindowFromMap(userValue);
            if (window == null) {
                window = new ChatMainWindow(socket,oos,userValue,currentUser);
                window.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
                window.setVisible(true);
                ManageUserWindowList.addUserWindowToMap(userValue,window);
                cst.setChatMainWindow(window);
            }else{
               window.setVisible(!window.isShowing());
            }
            if(window!=null&&window.isShowing()){
                ClientMessageQueue queue = cst.getQueue();
                CopyOnWriteArrayList<String> msgQueue = queue.getMsgQueue();
                if (msgQueue.size()==0){
                    return;
                }
                for (String s : msgQueue) {
                    window.setText(userValue.getUsername()+"说"+s);
                }
                queue.clearMsgQueue();
            }
        }
    }

    public void addDataToJlist(ArrayList<User> data) {
        FriListModel buddy = new FriListModel(data);
        list1.setModel(buddy);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - unknown
        panel2 = new JPanel();
        toolBar1 = new JToolBar();
        button1 = new JButton();
        button2 = new JButton();
        button3 = new JButton();
        panel1 = new JPanel();
        scrollPane1 = new JScrollPane();

        list1 = new JList();
        list1.setCellRenderer(new FriListCellRenderer());
        list1.setSelectionModel(new DefaultListSelectionModel(){
            @Override
            public void setSelectionInterval(int index0, int index1) {
                if(index0==index1){
                    if(isSelectedIndex(index0)){
                        removeSelectionInterval(index0,index0);
                        return;
                    }
                }
                super.setSelectionInterval(index0, index1);
            }

            @Override
            public void addSelectionInterval(int index0, int index1) {
                if(index0==index1){
                    if(isSelectedIndex(index0)){
                        removeSelectionInterval(index0,index0);
                        return;
                    }
                    super.addSelectionInterval(index0, index1);
                }

            }

        });
        list1.setFont(new Font(Font.SERIF, Font.PLAIN, 100));
        list1.setPreferredSize(new Dimension(110, 350));

        //======== this ========
        setMinimumSize(new Dimension(240, 439));
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== panel2 ========
        {
            panel2.setMinimumSize(new Dimension(240, 50));
            panel2.setPreferredSize(new Dimension(240, 50));
            panel2.setBorder(new javax.swing.border.CompoundBorder(new javax.swing.border.TitledBorder(new javax.swing.border.
                    EmptyBorder(0, 0, 0, 0), "JF\u006frmD\u0065sig\u006eer \u0045val\u0075ati\u006fn", javax.swing.border.TitledBorder.CENTER, javax.swing
                    .border.TitledBorder.BOTTOM, new Font("Dia\u006cog", Font.BOLD, 12),
                    Color.red), panel2.getBorder()));
            panel2.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
                @Override
                public void propertyChange(java.beans.PropertyChangeEvent e) {
                    if ("\u0062ord\u0065r".equals(e.getPropertyName()))
                        throw new RuntimeException();
                }
            });
            panel2.setLayout(new GridLayout());

            //======== toolBar1 ========
            {
                toolBar1.setMaximumSize(new Dimension(240, 50));
                toolBar1.setMinimumSize(new Dimension(240, 50));
                toolBar1.setPreferredSize(new Dimension(240, 50));

                //---- button1 ----
                button1.setText("\u597d\u53cb");
                button1.setFont(button1.getFont().deriveFont(button1.getFont().getSize() + 2f));
                button1.setMaximumSize(new Dimension(58, 40));
                button1.setMinimumSize(new Dimension(58, 40));
                button1.setPreferredSize(new Dimension(58, 40));
                button1.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        try {
                            Message message = new Message();
                            message.setMagType(MessageType.MESSAGE_GET_ONLINE_FRIENDS);
                            oos.writeObject(message);
                            selection=1;
                        } catch (IOException ioException) {
                            ioException.printStackTrace();
                        }
                    }
                });
                toolBar1.add(button1);

                //---- button2 ----
                button2.setText("\u7fa4");
                button2.setFont(button2.getFont().deriveFont(button2.getFont().getSize() + 2f));
                button2.setMaximumSize(new Dimension(58, 40));
                button2.setMinimumSize(new Dimension(58, 40));
                button2.setPreferredSize(new Dimension(58, 40));
                toolBar1.add(button2);

                //---- button3 ----
                button3.setText("\u6240\u6709\u4eba");
                button3.setFont(button3.getFont().deriveFont(button3.getFont().getSize() + 2f));
                button3.setMaximumSize(new Dimension(68, 40));
                button3.setMinimumSize(new Dimension(68, 40));
                button3.setPreferredSize(new Dimension(68, 40));
                button3.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        try {
                            Message message = new Message();
                            message.setMagType(MessageType.MESSAGE_GET_ALL_FRIENDS);
                            oos.writeObject(message);
                            selection=3;
                        } catch (IOException ioException) {
                            ioException.printStackTrace();
                        }
                    }
                });
                toolBar1.add(button3);
            }
            panel2.add(toolBar1);
        }
        contentPane.add(panel2, BorderLayout.NORTH);

        //======== panel1 ========
        {
            panel1.setMinimumSize(new Dimension(240, 439));
            panel1.setPreferredSize(new Dimension(240, 439));
            panel1.setLayout(new GridLayout());

            //======== scrollPane1 ========
            {
                scrollPane1.setMinimumSize(new Dimension(110, 439));
                scrollPane1.setPreferredSize(new Dimension(110, 439));

                //---- list1 ----
                list1.setMaximumSize(new Dimension(110, 439));
                list1.setMinimumSize(new Dimension(110, 439));
                list1.setPreferredSize(new Dimension(110, 439));
                list1.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 18));
                list1.addListSelectionListener(e -> list1ValueChanged(e));
                scrollPane1.setViewportView(list1);
            }
            panel1.add(scrollPane1);
        }
        contentPane.add(panel1, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - unknown
    private JPanel panel2;
    private JToolBar toolBar1;
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JPanel panel1;
    private JScrollPane scrollPane1;
    private JList list1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
